unit JudoFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBCtrls, StdCtrls, Grids, DBGrids, ExtCtrls, DB, DBClient,
  ComCtrls, RpCon, RpConDS, RpDefine, RpRave, JudoMod, XPMan, Menus,
  Buttons, JvCombobox, RpRender, RpRenderCanvas, RpRenderPreview, RpBase,
  RpFiler, RpSystem, JvDBCtrl;

type
  TIndexDirection = (idAscending, idDescending);

  TfrmJudo = class(TForm)
    boxMain: TPanel;
    boxFiltro: TPanel;
    btnCerca: TButton;
    Label3: TLabel;
    edtNome: TEdit;
    btnTogliFiltro: TButton;
    Label4: TLabel;
    cmbPalestra: TDBLookupComboBox;
    lblCategoria: TLabel;
    boxCommand: TPanel;
    navAtleti: TDBNavigator;
    navPalestre: TDBNavigator;
    Splitter1: TSplitter;
    lblElenco: TLabel;
    lblElencoPalestre: TLabel;
    popFunzioni: TPopupMenu;
    btnFunzioni: TBitBtn;
    itmPrintTab: TMenuItem;
    itmPrintElenco: TMenuItem;
    rptTabellone: TRvProject;
    cntTabellone32: TRvDataSetConnection;
    itmMescolaPalestre: TMenuItem;
    cntTabellone16: TRvDataSetConnection;
    cntTabellone8: TRvDataSetConnection;
    cntTabellone4: TRvDataSetConnection;
    lblCatPeso: TLabel;
    cmbCatPeso: TComboBox;
    itmPrintPropedeutico: TMenuItem;
    itmMescolaAtleti: TMenuItem;
    itmOrdinaPerPalestra: TMenuItem;
    chkSelezione: TCheckBox;
    cntAtleti: TRvDataSetConnection;
    N1: TMenuItem;
    btnTogliOrd: TButton;
    itmPrintItaliana: TMenuItem;
    cntTabelloneIta3: TRvDataSetConnection;
    cntTabelloneIta4: TRvDataSetConnection;
    cntTabelloneIta5: TRvDataSetConnection;
    itmPrintVerbalePeso: TMenuItem;
    itmCatPeso: TMenuItem;
    cmbCategoria: TJvComboBox;
    RvSystem: TRvSystem;
    actMainMenu: TMainMenu;
    IMenu: TMenuItem;
    itmAbout: TMenuItem;
    grpCinture: TGroupBox;
    chkBianca: TCheckBox;
    chkGialla: TCheckBox;
    chkArancio: TCheckBox;
    chkVerde: TCheckBox;
    chkMarrone: TCheckBox;
    chkNera: TCheckBox;
    chkBlu: TCheckBox;
    chkDan: TCheckBox;
    itmJudoColori: TMenuItem;
    itmJudoColoriProp: TMenuItem;
    btnSelectAll: TSpeedButton;
    itmSaveAll: TMenuItem;
    XPManifest1: TXPManifest;
    grdPalestre: TJvDBGrid;
    grdAtleti: TJvDBGrid;
    boxMove: TPanel;
    btnDown: TBitBtn;
    btnUp: TBitBtn;
    N2: TMenuItem;
    N3: TMenuItem;
    itmRicalcolaCodici: TMenuItem;
    cmbCategoria2: TJvComboBox;
    cntTabellone2: TRvDataSetConnection;
    procedure FormCreate(Sender: TObject);
    procedure btnCercaClick(Sender: TObject);
    procedure btnTogliFiltroClick(Sender: TObject);
    procedure itmPrintTabClick(Sender: TObject);
    procedure itmMescolaPalestreClick(Sender: TObject);
    procedure grdAtletiTitleClick(Column: TColumn);
    procedure grdPalestreTitleClick(Column: TColumn);
    procedure btnFunzioniClick(Sender: TObject);
    procedure itmPrintPropedeuticoClick(Sender: TObject);
    procedure itmMescolaAtletiClick(Sender: TObject);
    procedure itmOrdinaPerPalestraClick(Sender: TObject);
    procedure chkSelezioneClick(Sender: TObject);
    procedure itmPrintElencoClick(Sender: TObject);
    procedure btnTogliOrdClick(Sender: TObject);
    procedure grdAtletiKeyPress(Sender: TObject; var Key: Char);
    procedure itmPrintItalianaClick(Sender: TObject);
    procedure itmPrintVerbalePesoClick(Sender: TObject);
    procedure itmCatPesoClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cmbCatPesoEnter(Sender: TObject);
    procedure itmJudoColoriPropClick(Sender: TObject);
    procedure btnSelectAllClick(Sender: TObject);
    procedure itmSaveAllClick(Sender: TObject);
    procedure grdAtletiGetBtnParams(Sender: TObject; Field: TField;
      AFont: TFont; var Background: TColor; var SortMarker: TSortMarker;
      IsDown: Boolean);
    procedure grdPalestreGetBtnParams(Sender: TObject; Field: TField;
      AFont: TFont; var Background: TColor; var SortMarker: TSortMarker;
      IsDown: Boolean);
    procedure btnUpClick(Sender: TObject);
    procedure itmRicalcolaCodiciClick(Sender: TObject);
    procedure cmbCategoriaChange(Sender: TObject);
    procedure itmAboutClick(Sender: TObject);
  private
    FIndexDirection :TIndexDirection;
    FTitle: string;
    procedure SortBy(FieldName :String; DataSet :TDataSet);
    function GetFiltroCatPeso(AString :String) :String;
    procedure FillCatPesoList;
    procedure CheckCopyright;
    function GetCatPeso(AString :String) :String;
    procedure SetParametri;
    procedure Init();
    procedure SaveAll();
  public
    { Public declarations }
  end;

const
  LabelElenco = 'Elenco: ';
  TitleFileName = 'title.txt';  

var
  frmJudo: TfrmJudo;

implementation

{$R *.dfm}

uses
  Clipbrd, CatPesoFrm,
  Registry;

procedure ExportDataSet(DataSet :TDataSet);

  function GetTabbedRow: string;
  var
    i :Integer;
  begin
    Result := '';
    for i := 0 to DataSet.FieldCount - 1 do
      if DataSet.Fields[i].Visible then
      begin
        if result <> '' then
          result := result + #$9;
        result := result + DataSet.Fields[i].Text;
      end;
  end;

  function GetTabbedTitle: string;
  var
    i :Integer;
  begin
    Result := '';
    for i := 0 to DataSet.FieldCount - 1 do
      if DataSet.Fields[i].Visible then
      begin
        if result <> '' then
          result := result + #$9;
        result := result + DataSet.Fields[i].DisplayName;
      end;
  end;

var
  s :String;
  b :String;

begin
  if assigned(DataSet) and DataSet.Active then
  begin
    s := GetTabbedTitle + #13#10;
    b := DataSet.Bookmark;
    DataSet.DisableControls;
    Screen.Cursor := crHourGlass;
    try
      DataSet.First;
      while not DataSet.EOF do
      begin
        s := s + GetTabbedRow + #13#10;
        DataSet.Next;
      end;
      Clipboard.AsText := s;
      DataSet.Bookmark := b;
    finally
      Screen.Cursor := crDefault;
      DataSet.EnableControls;
    end;
  end;
end;

procedure TfrmJudo.FormCreate(Sender: TObject);
begin
  Init();
end;

procedure TfrmJudo.btnCercaClick(Sender: TObject);
var
  Filtro, FiltroCat, Bookmark, Codici, CintureList :String;
  i :Integer;
begin
  CintureList := '';
  Filtro := '1 = 1';
  if edtNome.Text <> '' then
    Filtro := Filtro + ' AND (Upper(Nome) like ' + QuotedStr('%' + UpperCase(edtNome.Text) + '%') + ')';

  if cmbPalestra.Text <> '' then
    Filtro := Filtro + 'AND (CodicePalestra = ' + VarToStr(cmbPalestra.KeyValue) + ')';

  if ((cmbCategoria.ItemIndex <> 0) and (cmbCategoria.ItemIndex <> -1)) or
    ((cmbCategoria2.ItemIndex <> 0) and (cmbCategoria2.ItemIndex <> -1)) then
  begin
    if (cmbCategoria.ItemIndex <> 0) and (cmbCategoria.ItemIndex <> -1) then
    begin
      if (cmbCategoria2.ItemIndex <> 0) and (cmbCategoria2.ItemIndex <> -1) then
        FiltroCat := FiltroCat + '((Categoria = ' + QuotedStr(cmbCategoria.Text) + ')'
      else
        FiltroCat := FiltroCat + '(Categoria = ' + QuotedStr(cmbCategoria.Text) + ')'      
    end;

    if (cmbCategoria2.ItemIndex <> 0) and (cmbCategoria2.ItemIndex <> -1) then
    begin
      if (cmbCategoria.ItemIndex <> 0) and (cmbCategoria.ItemIndex <> -1) then
        FiltroCat := FiltroCat + ' or (Categoria = ' + QuotedStr(cmbCategoria2.Text) + '))'
      else
        FiltroCat := FiltroCat + '(Categoria = ' + QuotedStr(cmbCategoria2.Text) + ')'
    end;

    Filtro := Filtro + 'AND ' + FiltroCat;
  end;

  if grpCinture.Visible then
  begin
    if chkBianca.Checked then
      CintureList := QuotedStr('B');

    if chkGialla.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('G');
    end;
    if chkArancio.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('A');
    end;
    if chkVerde.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('V');
    end;
    if chkBlu.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('U');
    end;
    if chkMarrone.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('M');
    end;
    if chkNera.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('N');
    end;
    if chkDan.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ', ';
      CintureList := CintureList + QuotedStr('D');
    end;

    if CintureList <> '' then
      Filtro := Filtro + 'AND Grado in (' + CintureList + ')';
  end;


  if grdAtleti.SelectedRows.Count > 0 then
  begin
    Bookmark := modJudo.cdsAtleti.Bookmark;
    modJudo.cdsAtleti.DisableControls;
    try
      Codici := '';
      for i := 0 to grdAtleti.SelectedRows.Count - 1 do
      begin
        modJudo.cdsAtleti.GotoBookmark(pointer(grdAtleti.SelectedRows.Items[i]));
        Codici := Codici + ' ' + modJudo.cdsAtletiCodice.AsString + ', ';
      end;
      Codici := Copy(Codici, 0, Length(Codici) - 2);
      Filtro := Filtro + 'AND Codice IN (' + Codici + ')';
      modJudo.cdsAtleti.Bookmark := Bookmark;
    finally
      modJudo.cdsAtleti.EnableControls;
    end;
  end;

  if cmbCatPeso.Text <> '' then
    Filtro := Filtro + 'AND ' + GetFiltroCatPeso(cmbCatPeso.Text);

  modJudo.SetFilterAtleti(Filtro);
  lblElencoPalestre.Caption := 'Palestre: ' + IntToStr(modJudo.cdsPalestre.RecordCount);
  lblElenco.Caption := LabelElenco + IntToStr(modJudo.cdsAtleti.RecordCount) + ' atleti';
end;

procedure TfrmJudo.btnTogliFiltroClick(Sender: TObject);
begin
  modJudo.TogliFiltro;
  edtNome.Text := '';
  cmbPalestra.KeyValue := null;
  cmbCategoria.Text := '';
  cmbCatPeso.Text := '';
  cmbCatPeso.Items.Clear;
  lblElencoPalestre.Caption := 'Palestre: ' + IntToStr(modJudo.cdsPalestre.RecordCount);
  lblElenco.Caption := LabelElenco + IntToStr(modJudo.cdsAtleti.RecordCount) + ' atleti';
end;

procedure TfrmJudo.itmMescolaPalestreClick(Sender: TObject);
begin
  modJudo.MescolaPalestre;
end;

procedure TfrmJudo.grdAtletiTitleClick(Column: TColumn);
begin
  if Column.FieldName <> 'Palestra' then
    SortBy(Column.FieldName, Column.Field.DataSet);
end;

procedure TfrmJudo.SortBy(FieldName: String; DataSet :TDataSet);
var
  IndexName :String;
begin
  inherited;
  IndexName := (DataSet as TClientDataSet).IndexName;
  if IndexName <> '' then
    (DataSet as TClientDataSet).DeleteIndex(IndexName);
  if FieldName = IndexName then
  begin
    if FIndexDirection = idAscending then
    begin
      (DataSet as TClientDataSet).AddIndex(FieldName, FieldName, [ixCaseInsensitive, ixDescending]);
      FIndexDirection := idDescending;
    end
    else
    begin
      (DataSet as TClientDataSet).AddIndex(FieldName, FieldName, [ixCaseInsensitive]);
      FIndexDirection := idAscending;
    end
  end
  else
  begin
    (DataSet as TClientDataSet).AddIndex(FieldName, FieldName, [ixCaseInsensitive]);
    FIndexDirection := idAscending;
  end;
  (DataSet as TClientDataSet).IndexName := FieldName;
  (DataSet as TClientDataSet).First;
end;

procedure TfrmJudo.grdPalestreTitleClick(Column: TColumn);
begin
  SortBy(Column.FieldName, Column.Field.DataSet);
end;

procedure TfrmJudo.btnFunzioniClick(Sender: TObject);
var
  ClientPoint, ScreenPoint :TPoint;
begin
  itmPrintTab.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmPrintElenco.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmMescolaPalestre.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmPrintPropedeutico.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmMescolaAtleti.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmOrdinaPerPalestra.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmPrintItaliana.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmPrintVerbalePeso.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmJudoColori.Enabled := not modJudo.cdsAtleti.IsEmpty;
  itmJudoColoriProp.Enabled := not modJudo.cdsAtleti.IsEmpty;
  
  ClientPoint.x := btnFunzioni.Left;
  ClientPoint.y := btnFunzioni.Top + btnFunzioni.Height;
  ScreenPoint := btnFunzioni.Parent.ClientToScreen(ClientPoint);
  popFunzioni.Popup(ScreenPoint.x, ScreenPoint.y);
end;

procedure TfrmJudo.itmPrintTabClick(Sender: TObject);
var
  i :Integer;
begin
  if modJudo.cdsAtleti.RecordCount > 32 then
    raise Exception.Create('Atleti troppo numerosi');

  modJudo.TogliOrdinamento;
  modJudo.MescolaPalestre;
  modJudo.MescolaAtleti;
  modJudo.Ordina;
  i := 1;
  modJudo.cdsAtleti.DisableControls;
  try
    modJudo.cdsAtleti.First;
    if (modJudo.cdsAtleti.RecordCount > 16) and (modJudo.cdsAtleti.RecordCount <= 32) then
    begin
      modJudo.cdsTabellone32.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone32.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone32.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report32');
    end
    else if (modJudo.cdsAtleti.RecordCount > 8) and (modJudo.cdsAtleti.RecordCount <= 16) then
    begin
      modJudo.cdsTabellone16.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone16.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone16.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report16');
    end
    else if (modJudo.cdsAtleti.RecordCount > 4) and (modJudo.cdsAtleti.RecordCount <= 8) then
    begin
      modJudo.cdsTabellone8.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone8.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone8.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report8');
    end
    else if (modJudo.cdsAtleti.RecordCount > 2) and (modJudo.cdsAtleti.RecordCount <= 4) then
    begin
      modJudo.cdsTabellone4.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone4.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone4.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report4');
    end
    else if modJudo.cdsAtleti.RecordCount = 2 then
    begin
      modJudo.cdsTabellone2.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone2.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone2.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report2');
    end
    else
      raise Exception.Create('Atleti troppo numerosi');
  finally
    modJudo.cdsAtleti.EnableControls;
  end;
end;

procedure TfrmJudo.itmPrintPropedeuticoClick(Sender: TObject);
var
  i :Integer;
begin
  if modJudo.cdsAtleti.RecordCount > 8 then
    raise Exception.Create('Atleti troppo numerosi');
  modJudo.TogliOrdinamento;;    
  modJudo.MescolaPalestre;
  modJudo.MescolaAtleti;
  modJudo.Ordina;
  i := 1;
  modJudo.cdsAtleti.DisableControls;
  try
    modJudo.cdsAtleti.First;
    if modJudo.cdsAtleti.RecordCount <= 8 then
    begin
      modJudo.cdsTabellone8.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone8.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone8.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report8Prop');
    end
    else
      raise Exception.Create('Atleti troppo numerosi');
  finally
    modJudo.cdsAtleti.EnableControls;
  end;
end;

procedure TfrmJudo.itmMescolaAtletiClick(Sender: TObject);
begin
  modJudo.MescolaAtleti;
end;

procedure TfrmJudo.itmOrdinaPerPalestraClick(Sender: TObject);
begin
  modJudo.Ordina;
end;

procedure TfrmJudo.chkSelezioneClick(Sender: TObject);
begin
  if chkSelezione.Checked then
    grdAtleti.Options := grdAtleti.Options + [dgMultiSelect]
  else
    grdAtleti.Options := grdAtleti.Options - [dgMultiSelect];
end;

procedure TfrmJudo.itmPrintElencoClick(Sender: TObject);
begin
  SetParametri;
  rptTabellone.ExecuteReport('Lista');
end;

procedure TfrmJudo.btnTogliOrdClick(Sender: TObject);
begin
  modJudo.TogliOrdinamento;
end;

procedure TfrmJudo.grdAtletiKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #3 then//  if Key = chr(VK_CONTROL) then
    ExportDataSet(modJudo.cdsAtleti);
end;

procedure TfrmJudo.itmPrintItalianaClick(Sender: TObject);
var
  i :Integer;
begin
  if modJudo.cdsAtleti.RecordCount > 5 then
    raise Exception.Create('Atleti troppo numerosi');

  i := 1;
  modJudo.TogliOrdinamento;
  modJudo.MescolaAtleti;
  modJudo.cdsAtleti.DisableControls;
  try
    modJudo.cdsAtleti.First;
    if modJudo.cdsAtleti.RecordCount <= 3 then
    begin
      modJudo.cdsTabelloneIta3.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabelloneIta3.FieldByName('Atleta' + IntToStr(i)).AsString := modJudo.cdsAtletiNome.AsString;
        modJudo.cdsTabelloneIta3.FieldByName('Palestra' + IntToStr(i)).AsString := modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabelloneIta3.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report3Ita');
    end
    else if modJudo.cdsAtleti.RecordCount = 4 then
    begin
      modJudo.cdsTabelloneIta4.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabelloneIta4.FieldByName('Atleta' + IntToStr(i)).AsString := modJudo.cdsAtletiNome.AsString;
        modJudo.cdsTabelloneIta4.FieldByName('Palestra' + IntToStr(i)).AsString := modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabelloneIta4.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report4Ita');
    end
    else if modJudo.cdsAtleti.RecordCount = 5 then
    begin
      modJudo.cdsTabelloneIta5.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabelloneIta5.FieldByName('Atleta' + IntToStr(i)).AsString := modJudo.cdsAtletiNome.AsString;
        modJudo.cdsTabelloneIta5.FieldByName('Palestra' + IntToStr(i)).AsString := modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabelloneIta5.Post;
      SetParametri;
      rptTabellone.ExecuteReport('Report5Ita');
    end;
    modJudo.cdsAtleti.First;
  finally
    modJudo.cdsAtleti.EnableControls;
  end;
end;

procedure TfrmJudo.itmPrintVerbalePesoClick(Sender: TObject);
begin
  SetParametri;
  rptTabellone.ExecuteReport('VerbalePeso');
end;

function TfrmJudo.GetFiltroCatPeso(AString: String): String;
var
  AValue, FirstValue, SecondValue: String;
  Min, Max :Boolean;
  i :Integer;
begin
  AValue := '';
  FirstValue := '';
  SecondValue := '';
  Min := False;
  Max := False;

  for i := 1 to Length(AString) do
  begin
    if (AString[i] <> '>') and (AString[i] <> '<') and (AString[i] <> '-') then
      AValue := AValue + AString[i]
    else if AString[i] = '-' then
    begin
      FirstValue := AValue;
      AValue := '';
    end
    else if AString[i] = '<' then
      Min := True
    else if AString[i] = '>' then
      Max := True;
  end;
  SecondValue := AValue;

  if not Min and not Max then
    Result := '(Peso >= ' + FirstValue + ' AND Peso <= ' + SecondValue + ')'
  else if Min then
    Result := '(Peso <= ' + AValue + ')'
  else if Max then
    Result := '(Peso >= ' + AValue + ')';
end;

procedure TfrmJudo.itmCatPesoClick(Sender: TObject);
begin
  frmCatPeso := TfrmCatPeso.Create(Self);
  try
    frmCatPeso.Title := FTitle;
    if frmCatPeso.ShowModal = mrOk then
    begin
      FillCatPesoList;
      FTitle := frmCatPeso.Title;
    end;
  finally
    frmCatPeso.Free;
  end;      
end;

procedure TfrmJudo.FillCatPesoList;
begin
  if cmbCategoria.ItemIndex = 0 then
    cmbCatPeso.Items.Text := ''
  else if cmbCategoria.ItemIndex = 1 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catBambini)
  else if cmbCategoria.ItemIndex = 2 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catBambini)
  else if cmbCategoria.ItemIndex = 3 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catRagazziM)
  else if cmbCategoria.ItemIndex = 4 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catRagazziF)
  else if cmbCategoria.ItemIndex = 5 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catEsAM)
  else if cmbCategoria.ItemIndex = 6 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catEsAF)
  else if cmbCategoria.ItemIndex = 7 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catEsBM)
  else if cmbCategoria.ItemIndex = 8 then
    cmbCatPeso.Items.Text := modJudo.GetCatPesoList(catEsBF);
end;

procedure TfrmJudo.CheckCopyright;
var
  Reg :TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.OpenKey('software', true);
    Reg.OpenKey('alsoft', true);
    Reg.OpenKey('judo', true);

    if not Reg.ValueExists('legalcopy') then
    begin
      MessageDlg('Impossibile avviare il programma.' + #13+#10 +
        'Contattare Alberto Dal Dosso 347 5421980.', mtError, [mbOK], 0);
      Close;
    end;
  finally
    Reg.Free;
  end;
end;

procedure TfrmJudo.FormShow(Sender: TObject);
begin
//  CheckCopyright;
end;

procedure TfrmJudo.cmbCatPesoEnter(Sender: TObject);
begin
  FillCatPesoList;
end;

procedure TfrmJudo.itmJudoColoriPropClick(Sender: TObject);
var
  i :Integer;
begin
  if modJudo.NumeroAtleti > 8 then
    raise Exception.Create('Atleti troppo numerosi');
  i := 1;
  modJudo.cdsAtleti.DisableControls;
  try
    modJudo.cdsAtleti.First;
    if modJudo.cdsAtleti.RecordCount <= 8 then
    begin
      modJudo.cdsTabellone8.Insert;
      while not modJudo.cdsAtleti.Eof do
      begin
        modJudo.cdsTabellone8.FieldByName('Atleta' + IntToStr(i)).AsString :=
          modJudo.cdsAtletiNome.AsString + ' - ' + modJudo.cdsAtletiPalestra.AsString;
        Inc(i);
        modJudo.cdsAtleti.Next;
      end;
      modJudo.cdsTabellone8.Post;
      SetParametri;
      if modJudo.NumeroAtleti = 5 then
        rptTabellone.ExecuteReport('Propedeutico5JC')
      else if modJudo.NumeroAtleti = 6 then
        rptTabellone.ExecuteReport('Propedeutico6JC')
      else
        rptTabellone.ExecuteReport('Report8Prop');
    end
    else
      raise Exception.Create('Atleti troppo numerosi');
  finally
    modJudo.cdsAtleti.EnableControls;
  end;
end;

procedure TfrmJudo.btnSelectAllClick(Sender: TObject);
begin
  chkBianca.Checked := btnSelectAll.Down;
  chkGialla.Checked := btnSelectAll.Down;
  chkArancio.Checked := btnSelectAll.Down;
  chkVerde.Checked := btnSelectAll.Down;
  chkBlu.Checked := btnSelectAll.Down;
  chkMarrone.Checked := btnSelectAll.Down;
  chkNera.Checked := btnSelectAll.Down;
  chkDan.Checked := btnSelectAll.Down;
end;

procedure TfrmJudo.itmSaveAllClick(Sender: TObject);
begin
  SaveAll();
end;

procedure TfrmJudo.grdAtletiGetBtnParams(Sender: TObject; Field: TField;
  AFont: TFont; var Background: TColor; var SortMarker: TSortMarker;
  IsDown: Boolean);
begin
  if Field.DataSet.Active and (Field <> nil) and
     (CompareText(Field.FieldName, (Field.DataSet as TClientDataSet).IndexName) = 0) Then
    if FIndexDirection = idDescending then
      SortMarker := smUp
    else
      SortMarker := smDown;
end;

procedure TfrmJudo.grdPalestreGetBtnParams(Sender: TObject; Field: TField;
  AFont: TFont; var Background: TColor; var SortMarker: TSortMarker;
  IsDown: Boolean);
begin
  if Field.DataSet.Active and (Field <> nil) and
     (CompareText(Field.FieldName, (Field.DataSet as TClientDataSet).IndexName)=0) Then
    if FIndexDirection = idDescending then
      SortMarker := smUp
    else
      SortMarker := smDown;
end;

function TfrmJudo.GetCatPeso(AString: String): String;
var
  i :Integer;
begin
  Result := '';
  if AString <> '' then
  begin
    for i := 1 to Length(AString) do
      if ((AString[i] >= '0') and (AString[i] <= '9')) or (AString[i] = ',') then
        Result := Result + AString[i]
      else
        Result := '';
    Result := IntToStr(Trunc(StrToFloat(Result)));
  end;
end;

procedure TfrmJudo.SetParametri;
var
  CintureList, Categoria :String;
begin
  if not btnSelectAll.Down then
  begin
    if chkBianca.Checked then
      CintureList := 'BI';
    if chkGialla.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - GI'
      else
        CintureList := 'GI';
    end;
    if chkArancio.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - AR'
      else
        CintureList := 'AR';
    end;
    if chkVerde.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - VE'
      else
        CintureList := 'VE';
    end;
    if chkBlu.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - BL'
      else
        CintureList := 'BL';
    end;
    if chkMarrone.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - MA'
      else
        CintureList := 'MA';
    end;
    if chkNera.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - NE'
      else
        CintureList := 'NE';
    end;
    if chkDan.Checked then
    begin
      if CintureList <> '' then
        CintureList := CintureList + ' - DA'
      else
        CintureList := 'DA';
    end;
  end;

  rptTabellone.SetParam('peso', 'Peso: ' + GetCatPeso(cmbCatPeso.Text));

  if (cmbCategoria.Text <> '') or (CintureList <> '')  then
  begin
    Categoria := 'Categoria: ';
    if cmbCategoria.Text <> '' then
      Categoria := Categoria + cmbCategoria.Text;
    if CintureList <> '' then
      Categoria := Categoria + '             ' + CintureList;
    rptTabellone.SetParam('categoria', Categoria);
  end;
  rptTabellone.SetParam('title', FTitle);
  rptTabellone.SetParam('numeroatleti', 'Totale atleti: ' + IntToStr(modJudo.NumeroAtleti));
end;

procedure TfrmJudo.btnUpClick(Sender: TObject);
begin
  modJudo.AtletiMoveUp;
end;

procedure TfrmJudo.itmRicalcolaCodiciClick(Sender: TObject);
begin
  modJudo.RicalcolaCodici;
end;

procedure TfrmJudo.cmbCategoriaChange(Sender: TObject);
begin
  cmbCatPeso.ItemIndex := -1;
end;

procedure TfrmJudo.itmAboutClick(Sender: TObject);
begin
  ;
end;

procedure TfrmJudo.Init;
var
  TitleFile: TStringList;
begin
  if FileExists(TitleFileName) then
  begin
    TitleFile := TStringList.Create;
    try
      TitleFile.LoadFromFile(TitleFileName);
      FTitle := TitleFile.Strings[0];
    finally
      TitleFile.Free;
    end;
  end
  else
    FTitle := 'Trofeo Judo Invorio';
  modJudo := TmodJudo.Create(self);
  grdAtleti.Columns.Items[2].DropDownRows := modJudo.cdsPalestre.RecordCount;
  cmbPalestra.DropDownRows := modJudo.cdsPalestre.RecordCount;
  modJudo.cdsTabellone32.CreateDataSet;
  modJudo.cdsTabellone16.CreateDataSet;
  modJudo.cdsTabellone8.CreateDataSet;
  modJudo.cdsTabellone4.CreateDataSet;
  modJudo.cdsTabellone2.CreateDataSet;  
  modJudo.cdsTabelloneIta3.CreateDataSet;
  modJudo.cdsTabelloneIta4.CreateDataSet;
  modJudo.cdsTabelloneIta5.CreateDataSet;
  modJudo.cdsPalestreApp.CreateDataSet;
  modJudo.cdsAtletiApp.CreateDataSet;
  if FileExists(ExtractFilePath(Application.ExeName) + 'Stampa.rav') then
    rptTabellone.ProjectFile := ExtractFilePath(Application.ExeName) + 'Stampa.rav'
  else
    raise Exception.Create('Manca il file Stampa.rav');

  lblElencoPalestre.Caption := 'Palestre: ' + IntToStr(modJudo.cdsPalestre.RecordCount);
  lblElenco.Caption := LabelElenco + IntToStr(modJudo.cdsAtleti.RecordCount) + ' atleti';
  FillCatPesoList;
  btnSelectAll.Down := True;
end;

procedure TfrmJudo.SaveAll;
var
  TitleFile: TStringList;
begin
  modJudo.SaveAll();
  TitleFile := TStringList.Create;
  try
    TitleFile.Text := FTitle;
    TitleFile.SaveToFile(TitleFileName);
  finally
    TitleFile.Free;
  end;  
end;

end.

